﻿Public Class Frm_chohoud
    Public avo As Temoins
    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Partager.isTelClicked = True
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_chohoud.Show()
        Partager.type = "Frm_Chohoud"
    End Sub

    Private Sub Frm_chohoud_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_cin.Select()
        Try
            Partager.moi.remplir_List_temoins()
            List_tous.Items.Clear()
            For Each khib As Temoins In Partager.moi.List_temoins
                List_tous.Items.Add(New ListViewItem(New String() {khib.Cin, khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة شاهد جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "") Then

                    For Each av As Temoins In Partager.moi.List_temoins
                        If av.Cin = txt_cin.Text And txt_cin.Text <> "" Then
                            MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If
                    Next
                    If MsgBox("هل تريد فعلا إضافة هذا الشاهد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة الشهود") = MsgBoxResult.Yes Then
                        Try
                            Partager.ControleNull(Me)
                            Partager.close()
                            Dim cli As New Temoins(1, txt_cin.Text, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, txt_adresse.Text, txt_molahadat.Text)
                            Partager.moi.Ajouter_temoins(cli)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_temoins.Count - 1
                            cli = CType(Partager.moi.List_temoins.Item(lastItem), Temoins)

                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next

                            List_tous.Items.Clear()
                            For Each khib As Temoins In Partager.moi.List_temoins
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Cin, khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                            Next
                            MsgBox("تمت إضافة الشاهد بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception

            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "") Then

                For Each av As Temoins In Partager.moi.List_temoins
                    If avo.Id <> av.Id Then
                        If av.Cin = txt_cin.Text And txt_cin.Text <> "" Then
                            MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                            Exit Sub
                        End If
                    End If
                Next

                If MsgBox("هل تريد فعلا تعديل معلومات هذا الشاهد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل الشهود") = MsgBoxResult.Yes Then
                    Try
                        Partager.ControleNull(Me)
                        Dim cli As New Temoins(1, txt_cin.Text, txt_3a2ili.Text, txt_chakhssi.Text, comb_ville.Text, txt_adresse.Text, txt_molahadat.Text)
                        Partager.moi.modifier_temoins(cli, avo.Id)
                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Or Partager.isTelClicked Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        List_tous.Items.Clear()
                        For Each khib As Temoins In Partager.moi.List_temoins
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Cin, khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
                    MsgBox("اختر شاهد من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الشاهد ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الشهود") = MsgBoxResult.Yes Then
                Try
                    Dim clt As Temoins = Partager.moi.List_temoins(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_temoins(avo.Id)
                    List_tous.Items.Clear()
                    For Each khib As Temoins In Partager.moi.List_temoins
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Cin, khib.Nom, khib.Prenom, khib.Ville, khib.Adress, khib.Remarque}))
                    Next
                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر شاهد من القائمة", MsgBoxStyle.Information, "حذف")
        End If
    End Sub


    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = Partager.moi.List_temoins(i)
                txt_cin.Text = avo.Cin
                txt_3a2ili.Text = avo.Nom
                txt_chakhssi.Text = avo.Prenom
                txt_adresse.Text = avo.Adress
                comb_ville.Text = avo.Ville
                txt_molahadat.Text = avo.Remarque
                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)
                    End If
                Next
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_cin.Select()
    End Sub

    Private Sub txt_cin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_cin.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub
End Class